<?php

$oid = makesafe($_REQUEST['campaign_id']);
$status = makesafe($_REQUEST['status']);

$network = 'Adworkmedia';

/*

http://YourSite.com/assets/postbacks/adworkmedia_postback.php

*/
$validIps = array('67.227.230.75', '"67.227.230.76');


$gid = safeGet($_GET['sid']);
$hash = safeGet($_GET['sid2']);

$_GET['ip'] = getIP();

$ip = getIP();

if(!in_array($ip, $validIps))
{
    die("You are not allowed to use this page!");	
}


 

if(isset($_REQUEST['campaign_id']) && !empty($gid)){
	
	

  $offer_id = makesafe($_REQUEST['campaign_id']);
  $sessId = makesafe($hash);
  

  //check if gateway is valid
  $gsql = mysql_query("SELECT gid, uid, unlock_period FROM gateways WHERE gid = '$gid'");
  if(!mysql_num_rows($gsql))
  die("Invalid Gateway");
  $gro = mysql_fetch_object($gsql);
  $aff_id = $gro->uid;
  $uid = $aff_id;
  $unlock_time = $gro->unlock_period;
   

  $min_offers = 1; 
  
  
  
  //get offer info
  
  $sql2 = mysql_query("SELECT * FROM gw_offers WHERE campaign_id = '$offer_id' AND active = 1 AND network = '$network' LIMIT 1");
  if(!mysql_num_rows($sql2))
  {
	  die("Invalid Offer");
  }
  
  $fro = mysql_fetch_object($sql2);
  $points = $fro->credits;
  $offerName = $fro->name;
  
  
  
	//Get user settings
    $usql = mysql_query("SELECT offer_rate FROM users WHERE uid = '$uid'");
	
	$creditMode = 'Default';
	
	$uro = mysql_fetch_object($usql);
	$user_offer_rate = stripslashes($uro->offer_rate);
	
	
	if(($user_offer_rate >= 0.01 && $user_offer_rate <= 100))
	{
        $creditMode = 'User';	
		$points = sprintf($points * ($user_offer_rate / 100));	
	}else
	{
		$points = sprintf($points * (OFFER_RATE / 100));	
	}
	
	

//end credit settings  
  
   
  
   if(empty($status))
   return;
  
    //reversal check   
	if($status == 2) //Status is not 1 it means offer not approved.
	{
		
		if(mysql_num_rows(mysql_query("SELECT id FROM gw_session_offers WHERE status = 'complete' AND session_id = '$sessId' AND gid = '$gid' AND offer_id = '$offer_id'")))
		{
			//@mysql_query("DELETE FROM earnings_log WHERE uid = '$uid' AND src_offer_id = '$offer_id' AND network = '$network' AND hash = '$sessId' AND notes LIKE 'Gateway%'");  //revoke user credit log		
			@mysql_query("UPDATE earnings_log SET credits = '0.00', notes = 'Reversed (content -$points)', date = NOW() WHERE uid = '$uid' AND src_offer_id = '$offer_id' AND network = '$network' AND hash = '$sessId' AND notes LIKE 'Gateway%'");  //revoke user credit log		

			@mysql_query("UPDATE users SET balance = balance-".$points." WHERE uid='".$uid."'");
			@mysql_query("DELETE FROM gw_stats WHERE gid = '$gid' AND offer_camp_id = '$offer_id' AND network = '$network' AND hash =  '$sessId')");	
		    @mysql_query("UPDATE gw_offers SET leads = leads-1, hits=hits-1 WHERE campaign_id = '$offer_id' AND network = '$network' LIMIT 1");	
            setGWEpc($oid, $network);
	
		}
	
			
		@mysql_query("UPDATE gw_session_offers SET status = 'reversed' WHERE session_id = '$sessId' AND gid = '$gid' AND offer_id = '$offer_id'");
		echo "Offer revoked";
		   exit;
	}  
  
  
  //Update Session offer to complete status
 if(mysql_query("UPDATE gw_session_offers SET status = 'complete' WHERE session_id = '$sessId' AND gid = '$gid' AND offer_id = '$offer_id' AND status = 'pending'")){

    //Now Count how many offers has been done.
	$count = mysql_num_rows(mysql_query("SELECT id FROM gw_session_offers WHERE session_id = '$sessId' AND gid = '$gid' AND status = 'complete'"));
	
	if($count >= $min_offers){
	
		if(mysql_num_rows(mysql_query("SELECT id FROM gw_sessions WHERE session_id = '$sessId' AND gid = '$gid'")))
		{
		
		
		    //Credit User / Add Earning Logs
			//end crediting user / earning log.
			
			if(mysql_query("UPDATE gw_sessions SET complete = 1 WHERE session_id = '$sessId' AND gid = '$gid'")){
			
			echo "Offer Complete!";		
			
            @mysql_query("UPDATE gw_offers SET leads = leads+1 WHERE campaign_id = '$offer_id' AND network = '$network' LIMIT 1");
			
			//set epc
			
            setGWEpc($oid, $network);
			
			//crediting user
             mysql_query("UPDATE users SET balance = balance+".$points." WHERE uid='".$uid."'") or die("Error occured while crediting user");
			 mysql_query("INSERT INTO `earnings_log` VALUES(NULL, '$uid', '0', '0', '$offer_id', '$offerName', '$points', 'Gateway Lead', NOW(), '$network', '$sessId')") or die("Problem...");
		
			//Adding GW earning Log
            @mysql_query("INSERT INTO gw_stats VALUES(NULL, '$gid', '$offer_id', '$points', NOW())");			
			

			
			
			
			
			}else
			die("Error.");
			
			
		}
	
	}
	
	
 } else {
	 
	 die("Error!");
	 
 }
  

}
?>

